const {AsyncParallelHook} = require('tapable');
//异步 说明回调函数里可以写异步代码 并行说明多个回调之间是并行执行的
class AsyncParallelHook111{
    constructor(args){
        this._args = args;//['name','age']
        this.taps = [];
    }
    tapPromise(name,fn){
        this.taps.push(fn);
    }
    promise(){
        let args = Array.from(arguments).slice(0,this._args.length);
        return Promise.all(this.taps.map(fn=>fn(...args)));
    }
}
let hook = new AsyncParallelHook(['name','age']);
hook.tapPromise('1',(name,age)=>{
   return new Promise(function(resolve,reject){
        setTimeout(() => {
            console.log('1',name,age);
            resolve();
        }, 1000);
   });
});
hook.tapPromise('2',(name,age)=>{
   return new Promise(function(resolve,reject){
        setTimeout(() => {
            console.log('2',name,age);
            //resolve();
            reject();
        }, 2000);
   });
});
let timer3;
hook.tapPromise('3',(name,age)=>{
    return new Promise(function(resolve,reject){
        timer3 = setTimeout(() => {
            console.log('3',name,age);
            resolve();
        }, 3000);
   });
});
console.time('cost');
hook.promise('zhufeng',10).then(result=>{
    console.timeEnd('cost');
    console.log(result);
},err=>{
    console.log(err);
    console.log('失败了');
    clearTimeout(timer3);
      console.timeEnd('cost');
});